//
//  PayMedthView.m
//  PropertyManager
//
//  Created by TNG on 2018/7/16.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "PayMedthView.h"

@interface PayMedthView() <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;
@property (strong, nonatomic) NSArray * titleArr;
@property (strong, nonatomic) NSArray * imgArr;
@property (strong, nonatomic) NSMutableArray * dataArr;

@end

@implementation PayMedthView

static NSString * PayMedthViewTableViewCellID = @"PayMedthViewTableViewCell";

+(PayMedthView *)shareView
{
    static PayMedthView * view = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        view = [[PayMedthView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        view.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.7];
    });
    return view;
}

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self creatTableview];
        [self registCell];
        [self addData];
    }
    return self;
}

-(void)addData
{
    for (int i = 0; i<self.titleArr.count; i++) {
        PayMedthodModel * model = [[PayMedthodModel alloc] init];
        model.title = [self.titleArr objectAtIndex:i];
        model.image = [self.imgArr objectAtIndex:i];
        model.type = i;
        if (i==0) {
            model.isSelect = YES;
        }else
        {
            model.isSelect = NO;
        }
        [self.dataArr addObject:model];
    }
    WeakSelf;
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.tableview reloadData];
    });
}

-(void)registCell
{
    [self.tableview registerClass:[PayMedthViewTableViewCell class] forCellReuseIdentifier:PayMedthViewTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT, WIDTH,180) style:UITableViewStyleGrouped];
        tableview.delegate = self;
        tableview.dataSource = self;
        tableview.scrollEnabled = NO;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self addSubview:self.tableview];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.imgArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PayMedthViewTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PayMedthViewTableViewCellID];
    if (self.dataArr.count) {
        [cell sendPayMedthodModel:[self.dataArr objectAtIndex:indexPath.row]];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    for (PayMedthodModel * model1 in self.dataArr) {
        model1.isSelect = NO;
    }
    PayMedthodModel * model = [self.dataArr objectAtIndex:indexPath.row];
    model.isSelect = YES;
    [self hiddenView];
    self.payMedthChooseTypeEventBlock(model.type);    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * bgview =[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 60)];
    bgview.backgroundColor = SFMWhiteColor;
    
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 15, WIDTH - 80, 30)];
    titleLabel.font = font(18);
    titleLabel.textColor = SFM51;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"请选择支付方式";
    [bgview addSubview:titleLabel];
    
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH - 40, 0, 40, 60)];
    [btn setImage:[UIImage imageNamed:@"wrongImg"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgview addSubview:btn];
    
    return bgview;
}

-(void)btnClick
{
    [self hiddenView];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}

-(void)showView
{
    WeakSelf;
    [[self lastWindow] addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.tableview.frame = CGRectMake(0, HEIGHT - 180 - kHomeLineH, WIDTH, 180);
    }];
}

-(void)hiddenView{
    WeakSelf;
    [[self lastWindow] addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.tableview.frame = CGRectMake(0, HEIGHT , WIDTH, 180);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

- (UIWindow *)lastWindow
{
    NSArray *windows = [UIApplication sharedApplication].windows;
    for(UIWindow *window in [windows reverseObjectEnumerator]) {
        if ([window isKindOfClass:[UIWindow class]] &&
            CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds))
            return window;
    }
    return [UIApplication sharedApplication].keyWindow;
}

-(NSArray *)titleArr
{
    if (_titleArr == nil) {
        _titleArr = [NSArray arrayWithObjects:@"支付宝支付",@"微信支付", nil];
    }
    return _titleArr;
}

-(NSArray *)imgArr
{
    if (_imgArr == nil) {
        _imgArr = [NSArray arrayWithObjects:[UIImage imageNamed:@"AliPay"],[UIImage imageNamed:@"WeChat"], nil];
    }
    return _imgArr;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hiddenView];
}

@end
